//
//  MQStatusOriginalView.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class MQStatusOriginalView: UIView {
    
    private var bottomConstraint:Constraint?
    
    var statusViewModel:MQStatusViewModel?{
        didSet{
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            if let urlString = statusViewModel?.status?.user?.profile_image_url{
                iconView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "avatar_default_small"))
            }
            contentLabel.text = statusViewModel?.status?.text
            timeLabel.text = statusViewModel?.createText
            sourceLabel.text = statusViewModel?.sourceText
            
            memberView.image = statusViewModel?.memberImage
            verifiedView.image = statusViewModel?.verifiedViewImage
            
            bottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0{
                pictureView.hidden = false
                pictureView.picUrls = picUrls
                self.snp_updateConstraints(closure: { (make) -> Void in
                  self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(MQStatusCellMargin).constraint
                })
                
            }else{
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                  self.bottomConstraint =  make.bottom.equalTo(contentLabel.snp_bottom).offset(MQStatusCellMargin).constraint
                })
            }
        }
    }
     override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(MQStatusCellMargin)
            make.leading.equalTo(self).offset(MQStatusCellMargin)
            make.size.equalTo(CGSizeMake(MQStatusCellIconSize, MQStatusCellIconSize))
        }
        
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-3)
            make.centerY.equalTo(iconView.snp_bottom).offset(-3)
        }
        
        // 名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(MQStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        // 会员
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(MQStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(MQStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        // 内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(MQStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -MQStatusCellMargin)
        }
        //图片
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.leading.equalTo(contentLabel)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraint = make.bottom.equalTo(pictureView).offset(MQStatusCellMargin).constraint
        }
    }
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    // 时间与来源的label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 10)
    // 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    // 认证的图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        return label
    }()
    
    //图片
    private lazy var pictureView:MQStatusPictureView = {
        let pictureView = MQStatusPictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
}
